namespace WebSMS;

[Route("[controller]/[action]")]
[Controller]
[Authorize]
public class RuleController : ControllerBase
{
    private readonly RuleService _ruleService;
    private readonly RuleDataLinkService _dataRuleService;
    public RuleController(RuleService ruleService,RuleDataLinkService userRule)
    {
        _ruleService = ruleService;
        _dataRuleService = userRule;
    }
    
    [HttpGet]
    public ResponseObject<PagedList<RulePo>> GetRules(int page,int size)
    {
        var list = _ruleService.GetRulesList(page, size);
       return new ResponseObject<PagedList<RulePo>>().Ok(list,"查询成功");
    }
    [HttpGet]
    public ResponseObject<List<RulePo>> GetRuleList()
    {
        var list = _ruleService.GetRulesList();
        return new ResponseObject<List<RulePo>>().Ok(list,"查询成功");
    }
    
    [HttpPost]
    public async Task<ResponseObject<bool>> AddRule([FromBody] RuleForm form)
    {
        var id = Guid.NewGuid();
        if (form == null)
        {
            return new ResponseObject<bool>().Fail("参数不能为空");
        }
        
        var rule = await _ruleService.AddRules(new RulePo(){Id = id,Name = form.Name, Description = form.Description});
        if (form.DataList.Count != 0)
        {
            foreach (var item in form.DataList)
            {
                await _dataRuleService.AddRuleData(false,id, item);
            }
        }
        return rule? new ResponseObject<bool>().Ok( "添加成功") : new ResponseObject<bool>().Fail("添加失败");
    }

    [HttpPut]
    public async Task<ResponseObject<bool>> UpdateRule([FromBody] RuleForm form,Guid id)
    {
        if (form == null)
        {
            return new ResponseObject<bool>().Fail("参数不能为空");
        }

        if (form.DataList.Count != 0)
        {
            int i = 0;
            foreach (var item in form.DataList)
            {
                await _dataRuleService.AddRuleData(i==0,id, item);
                i++;
            }
        }
        var rule = _ruleService.UpdateRules(new RulePo(){Id = id,Name = form.Name, Description = form.Description});
        return rule.Result? new ResponseObject<bool>().Ok(rule.Result, "修改成功") : new ResponseObject<bool>().Fail("修改失败");
    }

    [HttpDelete]
    public async Task<ResponseObject<bool>> ClearDataPermission(Guid ruleId)    
    {
        var link = await _dataRuleService.ClearDataLink(ruleId);
        return new ResponseObject<bool>().Ok("清除成功");
    }
    
    
    [HttpDelete]
    public ResponseObject<bool> DeleteRule(Guid id)
    {
        var result = _ruleService.DeleteRules(id);
        return result.Result? 
            new ResponseObject<bool>().Ok(result.Result, "删除成功") 
            : 
            new ResponseObject<bool>().Fail("删除失败,该角色存在绑定用户");
    }
    
    /**********************************************************************/
    // 菜单相关
    [HttpGet]
    public async Task<ResponseObject<IQueryable>> GetRuleMenuList()
    {
        var list = await _ruleService.GetRuleMenuList();
        if(list == null)
        {
            return new ResponseObject<IQueryable>().Fail("查询失败");
        }
        return new ResponseObject<IQueryable>().Ok(list,"查询成功");
    }

    [HttpPost]
    public async Task<ResponseObject<bool>> AddRuleMenu([FromBody] RuleMenuForm form)
    {
        if (form.MenuList.Count != 0)
        {
            foreach (var item in form.MenuList)
            {
               await _ruleService.AddRuleMenu(form.Id, item);
            }
        }
        return new ResponseObject<bool>().Ok("添加成功");
    }

    [HttpDelete]
    public async Task<ResponseObject<bool>> DeleteRuleMenu(Guid ruleId)
    {
        var result = await _ruleService.DeleteRuleMenu(ruleId);
        return result? new ResponseObject<bool>().Ok("删除成功") 
            : 
            new ResponseObject<bool>().Fail("删除失败");
    }

    [HttpGet]
    public async Task<ResponseObject<Object>> GetRuleDetails (Guid id)
    {
        var result = await _ruleService.GetRuleDetails(id);
        return new ResponseObject<Object>().Ok(result,"查询成功");
    }
}